package com.luoc;

import java.io.*;

public class IoDemo {
    public static void main(String[] args) {
        // 原路径
        String sourceDir = "C:\\Users\\28061\\Downloads\\测试.txt";
        String tarDir = "C:\\Users\\28061\\Downloads\\测试2.txt";
        copyByBufferStreamCharPref(sourceDir, tarDir);
    }

    /**
     * 通过字节流复制文件
     *
     * @param sourceDir
     * @param tarDir
     */
    public static void copyByStream(String sourceDir, String tarDir) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(sourceDir);
            fos = new FileOutputStream(tarDir);
            int byteRead;
            while ((byteRead = fis.read()) != -1) {
                fos.write(byteRead);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }


    }

    /**
     * 通过字节流缓冲区复制文件
     *
     * @param sourceDir
     * @param tarDir
     */
    public static void copyByStreamPref(String sourceDir, String tarDir) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(sourceDir);
            fos = new FileOutputStream(tarDir);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, length);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }


    }

    /**
     * 通过字符流缓冲区复制文件
     *
     * @param sourceDir
     * @param tarDir
     */
    public static void copyByCharPref(String sourceDir, String tarDir) {
        FileReader fileReader = null;
        FileWriter fileWriter = null;
        try {
            fileReader = new FileReader(sourceDir);
            fileWriter = new FileWriter(tarDir);
            char[] buffer = new char[1024];
            int length;
            while ((length = fileReader.read(buffer)) != -1) {
                fileWriter.write(buffer, 0, length);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }


    }


    /**
     * 通过字节流缓冲流复制文件
     *
     * @param sourceDir
     * @param tarDir
     */
    public static void copyByBufferStreamPref(String sourceDir, String tarDir) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            fis = new FileInputStream(sourceDir);
            fos = new FileOutputStream(tarDir);
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, length);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }


    }

    /**
     * 通过字符流缓冲流复制文件
     *
     * @param sourceDir
     * @param tarDir
     */
    public static void copyByBufferCharPref(String sourceDir, String tarDir) {
        FileReader fileReader = null;
        FileWriter fileWriter = null;
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            fileReader = new FileReader(sourceDir);
            fileWriter = new FileWriter(tarDir);
            br = new BufferedReader(fileReader);
            bw = new BufferedWriter(fileWriter);
            char[] buffer = new char[1024];
            int length;
            while ((length = br.read(buffer)) != -1) {
                bw.write(buffer, 0, length);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }


    }


    /**
     * 通过字节流转字符流缓冲流复制文件
     *
     * @param sourceDir
     * @param tarDir
     */
    public static void copyByBufferStreamCharPref(String sourceDir, String tarDir) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            fis = new FileInputStream(sourceDir);
            fos = new FileOutputStream(tarDir);
            //转化流
            InputStreamReader isr = new InputStreamReader(fis);
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            br = new BufferedReader(isr);
            bw = new BufferedWriter(osw);
            String str = null;
            while ((str = br.readLine()) != null) {
                bw.write(str);
                //换行
                bw.newLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }


}